package com.myjob.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myjob.dto.ProfileDto;
import com.myjob.entity.*;
import com.myjob.mapper.*;
import com.myjob.response.Result;
import com.myjob.service.BaseInfoService;
import com.myjob.service.ProfileService;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chenyuan
 * @date 2021/1/17 11:57
 */
@Service
@Slf4j
public class ProfileServiceImpl implements ProfileService {
	@Autowired
	private UserMapper userMapper;

	@Autowired
	private BaseInfoMapper baseInfoMapper;
	@Autowired
	private BaseInfoService baseInfoService;

	@Autowired
	private EducationMapper educationMapper;

	@Autowired
	private SkillMapper skillMapper;

	@Autowired
	private ProjectMapper projectMapper;

	@Autowired
	private WebsiteMapper websiteMapper;

	@Autowired
	private WorkMapper workMapper;

	@Override
	public Result selectProfileByUserId(String userId) {
		Result result = null;

		if (UtilValidate.isEmpty(userId)){
			return Result.failed("参数为空");
		}
		ProfileDto profileDto = new ProfileDto();

		// 基本信息
		BaseInfo baseInfo = (BaseInfo) baseInfoService.selectBaseInfoById(userId).getDatas();
		profileDto.setBaseInfo(baseInfo);

		// 教育经历
		QueryWrapper<Education> wrapper2 = new QueryWrapper<>();
		wrapper2.eq("user_id", userId);
		List<Education> educations = educationMapper.selectList(wrapper2);
		profileDto.setEducations(educations);

		// 项目经验
		QueryWrapper<Project> wrapper3 = new QueryWrapper<>();
		wrapper3.eq("user_id", userId);
		List<Project> projects = projectMapper.selectList(wrapper3);
		profileDto.setProjects(projects);

		// 技能信息
		QueryWrapper<Skill> wrapper4 = new QueryWrapper<>();
		wrapper4.eq("user_id", userId);
		List<Skill> skills = skillMapper.selectList(wrapper4);
		profileDto.setSkills(skills);

		// 个人网站
		QueryWrapper<Website> wrapper5 = new QueryWrapper<>();
		wrapper5.eq("user_id", userId);
		List<Website> websites = websiteMapper.selectList(wrapper5);
		profileDto.setWebsites(websites);

		// 工作经验
		QueryWrapper<Work> wrapper = new QueryWrapper<>();
		wrapper.eq("user_id", userId);
		List<Work> works = workMapper.selectList(wrapper);
		profileDto.setWorks(works);

		result = Result.succeed(profileDto, "查询成功");
		return result;
	}

	/**
	 * 教育经历
	 * @return
	 */
	@Override
	public Result updateEducationById(Education education) {
		Result result = null;

		if (UtilValidate.isEmpty(education)){
			return Result.failed("参数为空");
		}

		int update = educationMapper.updateById(education);
		if (update <= 0){
			return Result.failed("修改失败");
		}
		result = Result.succeed("修改成功");
		return result;
	}

	@Override
	public Result deleteEducationById(String educationId) {
		Result result = null;

		if (UtilValidate.isEmpty(educationId)){
			return Result.failed("参数为空");
		}

		int delete = educationMapper.deleteById(educationId);
		if (delete <= 0){
			return Result.failed("删除失败");
		}
		result = Result.succeed("删除成功");
		return result;
	}

	@Override
	public Result addEducation(Education education) {
		Result result = null;

		if (UtilValidate.isEmpty(education)){
			return Result.failed("参数为空");
		}

		int insert = educationMapper.insert(education);
		if (insert <= 0){
			return Result.failed("添加失败");
		}
		result = Result.succeed("添加成功");
		return result;
	}


	/**
	 * 项目经验
	 * @return
	 */
	@Override
	public Result updateProjectById(Project project) {
		Result result = null;

		if (UtilValidate.isEmpty(project)){
			return Result.failed("参数为空");
		}

		int update = projectMapper.updateById(project);
		if (update <= 0){
			return Result.failed("修改失败");
		}
		result = Result.succeed("修改成功");
		return result;
	}

	@Override
	public Result deleteProjectById(String projectId) {
		Result result = null;

		if (UtilValidate.isEmpty(projectId)){
			return Result.failed("参数为空");
		}

		int delete = projectMapper.deleteById(projectId);
		if (delete <= 0){
			return Result.failed("删除失败");
		}
		result = Result.succeed("删除成功");
		return result;
	}

	@Override
	public Result addProject(Project project) {
		Result result = null;

		if (UtilValidate.isEmpty(project)){
			return Result.failed("参数为空");
		}

		int insert = projectMapper.insert(project);
		if (insert <= 0){
			return Result.failed("添加失败");
		}
		result = Result.succeed("添加成功");
		return result;
	}

	/**
	 * 技能
	 * @return
	 */
	@Override
	public Result updateSkillById(Skill skill) {
		Result result = null;

		if (UtilValidate.isEmpty(skill)){
			return Result.failed("参数为空");
		}

		int update = skillMapper.updateById(skill);
		if (update <= 0){
			return Result.failed("修改失败");
		}
		result = Result.succeed("修改成功");
		return result;
	}

	@Override
	public Result deleteSkillById(String skillId) {
		Result result = null;

		if (UtilValidate.isEmpty(skillId)){
			return Result.failed("参数为空");
		}

		int delete = skillMapper.deleteById(skillId);
		if (delete <= 0){
			return Result.failed("删除失败");
		}
		result = Result.succeed("删除成功");
		return result;
	}

	@Override
	public Result addSkill(Skill skill) {
		Result result = null;

		if (UtilValidate.isEmpty(skill)){
			return Result.failed("参数为空");
		}

		int insert = skillMapper.insert(skill);
		if (insert <= 0){
			return Result.failed("添加失败");
		}
		result = Result.succeed("添加成功");
		return result;
	}

	/**
	 * 个人网站
	 * @return
	 */
	@Override
	public Result updateWebsiteById(Website website) {
		Result result = null;

		if (UtilValidate.isEmpty(website)){
			return Result.failed("参数为空");
		}

		int update = websiteMapper.updateById(website);
		if (update <= 0){
			return Result.failed("修改失败");
		}
		result = Result.succeed("修改成功");
		return result;
	}

	@Override
	public Result deleteWebsiteById(String websiteId) {
		Result result = null;

		if (UtilValidate.isEmpty(websiteId)){
			return Result.failed("参数为空");
		}

		int delete = websiteMapper.deleteById(websiteId);
		if (delete <= 0){
			return Result.failed("删除失败");
		}
		result = Result.succeed("删除成功");
		return result;
	}

	@Override
	public Result addWebsite(Website website) {
		Result result = null;

		if (UtilValidate.isEmpty(website)){
			return Result.failed("参数为空");
		}

		int insert = websiteMapper.insert(website);
		if (insert <= 0){
			return Result.failed("添加失败");
		}
		result = Result.succeed("添加成功");
		return result;
	}

	/**
	 * 工作经验
	 * @return
	 */
	@Override
	public Result updateWorkById(Work work) {
		Result result = null;

		if (UtilValidate.isEmpty(work)){
			return Result.failed("参数为空");
		}

		int update = workMapper.updateById(work);
		if (update <= 0){
			return Result.failed("修改失败");
		}
		result = Result.succeed("修改成功");
		return result;
	}

	@Override
	public Result deleteWorkById(String workId) {
		Result result = null;

		if (UtilValidate.isEmpty(workId)){
			return Result.failed("参数为空");
		}

		int delete = workMapper.deleteById(workId);
		if (delete <= 0){
			return Result.failed("删除失败");
		}
		result = Result.succeed("删除成功");
		return result;
	}

	@Override
	public Result addWork(Work work) {
		Result result = null;

		if (UtilValidate.isEmpty(work)){
			return Result.failed("参数为空");
		}

		int insert = workMapper.insert(work);
		if (insert <= 0){
			return Result.failed("添加失败");
		}
		result = Result.succeed("添加成功");
		return result;
	}
}
